﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TetrisGame
{
    internal class Map : IDraw
    {
        // 装载墙壁对象的容器
        private readonly List<DrawObject> walls = new List<DrawObject>();

        // 装载方块对象的容器
        public readonly List<DrawObject> dynamicWalls = new List<DrawObject>();

        // 记录小方块行号的容器
        private readonly int[] recordInfo;

        // 记录有效行号的容器
        private readonly int width = 0;

        private GameScene scene;

        #region 使用构造函数生成墙壁

        public Map(GameScene scene)
        {
            this.scene = scene; // 关联当前场景
            // 初始化记录行号的容器最大值
            recordInfo = new int[Game.Height - 7];
            // 生成下墙壁
            for (int i = 0; i < Game.Width; i += 2)
            {
                walls.Add(new DrawObject(i, Game.Height - 7, E_DrawType.Wall)); // 下墙壁
                walls.Add(new DrawObject(i, Game.Height - 1, E_DrawType.Wall));
                width++; // 记录下墙壁的宽度
            }
            // 生成左右墙壁
            for (int i = 0; i < Game.Height; i++)
            {
                walls.Add(new DrawObject(0, i, E_DrawType.Wall));
                walls.Add(new DrawObject(Game.Width - 2, i, E_DrawType.Wall));
            }
            // 设置分数和下一个文字位置
            walls.Add(new DrawObject(3, Game.Height - 4, E_DrawType.NextTitle)); // 下一个文字位置
            walls.Add(new DrawObject(Game.Width / 2, Game.Height - 4, E_DrawType.Score)); // 分数文字位置
        }

        #endregion 使用构造函数生成墙壁

        public void Draw()
        {
            // 绘制地图中的所有墙壁
            if (walls.Count > 0)
            {
                foreach (DrawObject wall in walls)
                {
                    wall.Draw(); // 绘制墙壁
                }
            }
            // 绘制动态墙壁
            if (dynamicWalls.Count > 0)
            {
                foreach (DrawObject wall in dynamicWalls)
                {
                    wall.Draw(); // 绘制动态墙壁
                }
            }
        }

        public void Clear()
        {
            // 清除所有动态墙壁
            if (dynamicWalls.Count > 0)
            {
                foreach (DrawObject wall in dynamicWalls)
                {
                    wall.Clear();
                }
            }
        }

        /// <summary>
        /// 添加动态墙壁
        /// </summary>
        /// <param name="walls"></param>
        public void AddDynamicWall(List<DrawObject> walls)
        {
            for (int i = 0; i < walls.Count; i++)
            {
                walls[i].ChangeType(E_DrawType.Wall);
                // 将每个墙壁对象添加到动态墙壁列表中
                dynamicWalls.Add(walls[i]);

                if (walls[i].Pos.y <= 0)
                {
                    scene.Stop(); // 如果有小方块到达顶部，停止游戏
                    Game.ChangeScene(E_SceneType.End); // 切换到结束场景
                    return;
                }

                // 记录小方块在哪一行 有多少个
                recordInfo[Game.Height - 7 - walls[i].Pos.y - 1] += 1; // 记录该行有多少个小方块
            }
            ClearOneLine(); // 检测是否有满的一行
        }

        /// <summary>
        /// 清空一行
        /// </summary>
        // 优化行消除方法
        public void ClearOneLine()
        {
            List<DrawObject> toRemove = new List<DrawObject>();
            List<DrawObject> toMove = new List<DrawObject>();

            for (int k = 0; k < recordInfo.Length; k++)
            {
                if (recordInfo[k] == width - 2)
                {
                    Clear();
                    // 收集要移除和移动的方块
                    foreach (var wall in dynamicWalls)
                    {
                        int lineIndex = Game.Height - 7 - wall.Pos.y - 1;
                        if (lineIndex == k)
                        {
                            toRemove.Add(wall);
                        }
                        else if (lineIndex > k)
                        {
                            toMove.Add(wall);
                        }
                    }

                    // 更新记录数组
                    for (int i = k; i < recordInfo.Length - 1; i++)
                    {
                        recordInfo[i] = recordInfo[i + 1];
                    }
                    recordInfo[recordInfo.Length - 1] = 0;

                    // 一次性移动方块
                    foreach (var wall in toMove)
                    {
                        wall.Pos = new Position(wall.Pos.x, wall.Pos.y + 1);
                    }

                    // 移除方块
                    foreach (var item in toRemove)
                    {
                        dynamicWalls.Remove(item);
                    }
                    k--; // 重新检查当前行
                }
            }
        }
    }
}